/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.command.impl;

import org.soupframework.gwt.designerpanel.client.designer.ContainerDesigner;
import org.soupframework.gwt.designerpanel.client.designer.OperatingMode;
import org.soupframework.gwt.designerpanel.client.designer.impl.VerticalPanelViewDesigner;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class VerticalPanelDesignerCommandBase<T> extends CellPanelDesignerCommandBase<T> {

	public VerticalPanelDesignerCommandBase(ContainerDesigner<T> designer) {
		super(designer);
	}

	public VerticalPanelViewDesigner<T> getDesigner(){
		return (VerticalPanelViewDesigner<T>)super.getDesigner();
	}

	/**
	 * Exchange the given widgets.
	 *
	 * @param widget1 the first widget.
	 * @param widget2 the second widget.
	 */
	protected void exchangeWidgets(Widget widget1, Widget widget2){
		if(this.getDesigner().getManager().getOperatingMode() == OperatingMode.LIVE_MODE){
			return;
		}

		VerticalPanel hp = getDesigner().getObjectView();
		int i1 = hp.getWidgetIndex(widget1);
		hp.remove(i1);
		int i2 = hp.getWidgetIndex(widget2);
		hp.insert(widget1, i2);
		hp.remove(i2 + 1);
		hp.insert(widget2, i1);
	}
}
